package com.yuelao.yuelao_backend.controller;

import com.yuelao.yuelao_backend.common.ApiResponse;
import com.yuelao.yuelao_backend.dto.UserDTO;
import com.yuelao.yuelao_backend.entity.UserVisitor;
import com.yuelao.yuelao_backend.service.UserService;
import com.yuelao.yuelao_backend.service.UserVisitorService;
import com.yuelao.yuelao_backend.util.UserContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/visitors")
@Slf4j
public class VisitorController {

    @Resource
    private UserVisitorService userVisitorService;
    
    @Resource
    private UserService userService;

    /**
     * 获取访客列表
     * GET /visitors
     * @param page 页码，默认1
     * @param size 每页数量，默认10
     * @return 访客列表
     */
    @GetMapping
    public ApiResponse<List<UserDTO>> getVisitors(@RequestParam(defaultValue = "1") int page,
                                                  @RequestParam(defaultValue = "10") int size) {
        Long userId = UserContextUtil.getCurrentUserId();
        
        // 获取访客记录
        List<UserVisitor> visitorRecords = userVisitorService.getMyVisitors(userId, page, size);
        
        // 转换为UserDTO列表
        List<UserDTO> visitors = visitorRecords.stream()
                .map(visitor -> userService.getUserInfo(visitor.getVisitorUserId()))
                .collect(Collectors.toList());
        
        return ApiResponse.ok(visitors);
    }
}